package com.wltt.product.ahuthr.service;

import com.wltt.product.ahuthr.config.exception.BusinessException;
import com.wltt.product.ahuthr.constants.YearConstant;
import com.wltt.product.ahuthr.enums.ImportTypeEnum;
import com.wltt.product.ahuthr.excel.HouseFundListener;
import com.wltt.product.ahuthr.excel.SalaryLevelListener;
import com.wltt.product.ahuthr.executors.ImportHouseFundExecutor;
import com.wltt.product.ahuthr.executors.ImportSalaryLevelExecutor;
import com.wltt.product.ahuthr.executors.WorkStealingPool;
import com.wltt.product.ahuthr.mapper.*;
import com.wltt.product.ahuthr.model.ResultData;
import com.wltt.product.ahuthr.model.po.*;
import com.wltt.product.ahuthr.service.base.BaseServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;


/**
 * @ClassName: SalaryLevelServiceImpl
 * @Description: 第三部分薪资：薪级工资 逻辑服务类<br/>
 * 不做业务校验，参数校验统一放到前置的Service中
 * @Date: 2024-05-30 14:34:21
 * @Author yanggl
 */
@Service(value = "salaryLevelService")
public class SalaryLevelServiceImpl extends BaseServiceImpl<SalaryLevelPO, Long> implements SalaryLevelService {

    private SalaryLevelMapper salaryLevelMapper;

    @Resource
    private ImportRecordMapper importRecordMapper;
    @Resource
    private UserBasicMapper userBasicMapper;

    @Resource
    private SalaryMapper salaryMapper;

    @Resource
    private SystemLevelSalaryMapper systemLevelSalaryMapper;
    @Resource
    private SystemPostTypeMapper systemPostTypeMapper;

    @Resource
    private SalarySubsidyMapper salarySubsidyMapper;

    @Value("${file.error.path: ./error}")
    private String errorPath;

    //构造方法
    public SalaryLevelServiceImpl(SalaryLevelMapper salaryLevelMapper) {
        super(salaryLevelMapper);
        this.salaryLevelMapper = salaryLevelMapper;
    }

    /**
     * 导入薪级
     *
     * @param file
     * @return
     */
    @Override
    public ResultData importSalaryLevel(MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            throw new BusinessException("未上传文件", HttpStatus.BAD_REQUEST);
        }

        String fileName = file.getOriginalFilename();
        if (!(fileName.endsWith(".xls") || fileName.endsWith(".xlsx"))) {
            throw new BusinessException("请上传excel文件", HttpStatus.BAD_REQUEST);
        }

        ImportRecordPO importRecordPO = new ImportRecordPO();
        importRecordPO.setImportType(ImportTypeEnum.SALARY_LEVEL.getCode());
        importRecordPO.setImportDate(new Date());
        importRecordMapper.insertSelective(importRecordPO);

        File errorFile = new File(errorPath + "/" + System.currentTimeMillis() + ".xlsx");
        SalaryLevelListener salaryLevelListener = new SalaryLevelListener(this, userBasicMapper, salaryMapper, salaryLevelMapper, importRecordMapper, importRecordPO, errorFile);
        ImportSalaryLevelExecutor importSalaryLevelExecutor = new ImportSalaryLevelExecutor(file.getInputStream(), salaryLevelListener);
        WorkStealingPool.execute(importSalaryLevelExecutor);

        return ResultData.success();

    }

    /**
     * 更新薪级工资
     * TODO
     *
     * @param salaryId
     * @param postTypeId
     * @param level
     * @param levelSalary
     * @param salary10
     * @return
     */
    @Override
    @Transactional
    public ResultData updateSalaryLevelSevel(Long salaryId, Long postTypeId, Integer level, Double levelSalary, Double salary10) {

        SystemPostTypePO systemPostTypePO = systemPostTypeMapper.queryById(postTypeId);
        if (null == systemPostTypePO) {
            throw new BusinessException("工资系列不存在", HttpStatus.BAD_REQUEST);
        }

        SystemLevelSalaryPO systemLevelSalaryPO = systemLevelSalaryMapper.queryByPostTypeIdAndRank(postTypeId, level, YearConstant.YEAR_2014);
        if (null == systemLevelSalaryPO) {
            throw new BusinessException("薪级不存在", HttpStatus.BAD_REQUEST);
        }

        String postDescribe = systemPostTypePO.getPostTypeName();
        String levelDescribe = "系列：" + postDescribe + ",薪级: " + level;

        // 调整薪级
        SalaryLevelPO salaryLevelPO = salaryLevelMapper.queryBySalaryId(salaryId);
        if (null != salaryLevelPO) {
            // 修改
            salaryLevelPO.setPostTypeId(systemPostTypePO.getId());
            salaryLevelPO.setPostDescribe(postDescribe);
            salaryLevelPO.setLevelId(systemLevelSalaryPO.getId());
            salaryLevelPO.setLevelDescribe(levelDescribe);
            salaryLevelPO.setLevelSalary(BigDecimal.valueOf(levelSalary));
            salaryLevelMapper.updateById(salaryLevelPO);
        } else {
            // 新增
            salaryLevelPO = new SalaryLevelPO();
            salaryLevelPO.setSalaryId(salaryId);
            salaryLevelPO.setPostTypeId(systemPostTypePO.getId());
            salaryLevelPO.setPostDescribe(postDescribe);
            salaryLevelPO.setLevelId(systemLevelSalaryPO.getId());
            salaryLevelPO.setLevelDescribe(levelDescribe);
            salaryLevelPO.setLevelSalary(BigDecimal.valueOf(levelSalary));
            salaryLevelPO.setRemark("批量导入");
            salaryLevelMapper.insertSelective(salaryLevelPO);
        }

        // 调整工资10%
        SalarySubsidyPO salarySubsidyPO = salarySubsidyMapper.queryBySalaryId(salaryId);
        if (null != salarySubsidyPO) {
            salarySubsidyPO.setSalary10(BigDecimal.valueOf(salary10));
            salarySubsidyMapper.updateById(salarySubsidyPO);
        } else {
            salarySubsidyPO = new SalarySubsidyPO();
            salarySubsidyPO.setSalaryId(salaryId);
            salarySubsidyPO.setSalary10(BigDecimal.valueOf(salary10));
            salarySubsidyMapper.insertSelective(salarySubsidyPO);
        }

        return ResultData.success();
    }
}